import java.awt.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.Scanner;

import org.jfree.chart.*;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.DefaultKeyedValues;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.*;


public class ChartGenerator {

	
	static final private double[] arrTScoreStd= {0.99,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0.0};

	private int[] arrTBestCnt;

	private int[] arrTWorstCnt;

	private int nCaseAll;
	

	public void print() {

		System.err.println(arrTBestCnt.toString());
		System.err.println(arrTWorstCnt.toString());
		System.err.println(nCaseAll);
		
	}

	
	public void readTBestTWorst(InputStream istTBestTWorst){

		arrTBestCnt = new int[11];

		arrTWorstCnt = new int[11];
		
		nCaseAll = 0;		

		Scanner scScanner = new Scanner(istTBestTWorst);

		while (scScanner.hasNextInt()){
			
			nCaseAll++;

			Integer dTBestRank = scScanner.nextInt(); 			
			Integer dTBestAll = scScanner.nextInt(); 
			Double dTBestScore = 1-(double)dTBestRank/dTBestAll;
			
			for(int i=0; i < arrTScoreStd.length; i++){
				if(dTBestScore > arrTScoreStd[i]){
					arrTBestCnt[i]++;
				}
			}
			
			Integer dTWorstRank = scScanner.nextInt(); 			
			Integer dTWorstAll = scScanner.nextInt(); 	
			Double dTWorstScore = 1- (double)dTWorstRank/dTWorstAll;
			for(int i=0; i < arrTScoreStd.length; i++){
				if(dTWorstScore > arrTScoreStd[i]){
					arrTWorstCnt[i]++;
				}
			}
		
		}
	}
	

	private JFreeChart generateJFreeChat() throws IOException {

		DefaultKeyedValues kvsTBest = new DefaultKeyedValues();
		DefaultKeyedValues kvsTWorst = new DefaultKeyedValues();

		for(int i=0; i < arrTScoreStd.length; i++){
			kvsTBest.addValue( (Integer)(int)(arrTScoreStd[i]*100),(double)100*arrTBestCnt[i]/nCaseAll);
			kvsTWorst.addValue( (Integer)(int)(arrTScoreStd[i]*100),(double)100*arrTWorstCnt[i]/nCaseAll);			
		}
		
	    
	    
	    CategoryDataset cdTBest = DatasetUtilities.createCategoryDataset("HeuristicITBest", kvsTBest);
	    CategoryDataset cdTWorst = DatasetUtilities.createCategoryDataset("HeuristicITWorst", kvsTWorst);
	    CategoryDataset[] paramArrayOfCategoryDataset = new CategoryDataset[]{ cdTBest, cdTWorst };
		
		
		////////////////////////

	    JFreeChart localJFreeChart = ChartFactory.createLineChart("Evaluation of fault localization technique", 
	    		"% of program that need not be examined ('Score')", "% of test runs",
	    		null, PlotOrientation.VERTICAL,
	    		true,
	    		true, 
	    		false);
	    
	    
	    localJFreeChart.setBackgroundPaint(Color.white);
	    CategoryPlot localCategoryPlot = (CategoryPlot)localJFreeChart.getPlot();
	    
	    
	    localCategoryPlot.setBackgroundPaint(Color.white);
	    localCategoryPlot.setRangeGridlinePaint(Color.black);
	    CategoryAxis localCategoryAxis = localCategoryPlot.getDomainAxis();
	    localCategoryAxis.setLowerMargin(0.02D);
	    localCategoryAxis.setUpperMargin(0.02D);	    
	    localCategoryPlot.getRangeAxis().setUpperBound(103);
	    localCategoryPlot.getRangeAxis().setLowerBound(-3);
	    
	    
	    
	    
	    localCategoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
	    NumberAxis localNumberAxis1 = (NumberAxis)localCategoryPlot.getRangeAxis();
	    localNumberAxis1.setStandardTickUnits(NumberAxis.createStandardTickUnits());	    
	    
	    LineAndShapeRenderer localLineAndShapeRenderer = new LineAndShapeRenderer();
	    LineAndShapeRenderer localLineAndShapeRenderer2 = new LineAndShapeRenderer();
	    

	    localCategoryPlot.setDataset(0, paramArrayOfCategoryDataset[0]);
	    localCategoryPlot.setRenderer(0, localLineAndShapeRenderer);
	    localCategoryPlot.mapDatasetToRangeAxis(0, 0);
	    
	    localCategoryPlot.setDataset(1, paramArrayOfCategoryDataset[1]);
	    localCategoryPlot.setRenderer(1, localLineAndShapeRenderer2);
	    localCategoryPlot.mapDatasetToRangeAxis(0, 0);
	    
	    localCategoryPlot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
	    return localJFreeChart;

	}

	
	

	public static void main(String[] args) throws IOException {
		
		ChartGenerator gGenerator = new ChartGenerator();

		/**Input TBestTWorst File Stream*/
		FileInputStream fiTBestTWorst = new FileInputStream(args[0]);
		gGenerator.readTBestTWorst(fiTBestTWorst);		
		gGenerator.print();		
		JFreeChart chart = gGenerator.generateJFreeChat();  
		ChartUtilities.writeChartAsPNG(System.out, chart, 800, 600);

		
	}
	
}
